#include "xqat_weather.h"

xqat_weather::xqat_weather()
{
}

void xqat_weather::parse_json_data(struct weather_t *weather, QString json)
{
    QJsonParseError err;
    QJsonDocument json_recv = QJsonDocument::fromJson(json.toUtf8(), &err); //解析json对象
    if(!json_recv.isNull()) {
        QJsonObject object = json_recv.object();
        if(object.contains("data")) {
            QJsonValue value = object.value("data");  // 获取指定 key 对应的 value
            if(value.isObject()) {
                QJsonObject object_data = value.toObject();
                if(object_data.contains("forecast")) {
                    QJsonValue value = object_data.value("forecast");
                    if(value.isArray()) {
                        /* today */
                        QJsonObject today_weather = value.toArray().at(0).toObject();
                        weather->today.date = today_weather.value("date").toString();
                        weather->today.type = today_weather.value("type").toString();
                        weather->today.low = today_weather.value("low").toString();
                        weather->today.high = today_weather.value("high").toString();
                        weather->today.low = weather->today.low.mid(weather->today.low.length() - 3, 4) ;
                        weather->today.high = weather->today.high.mid(weather->today.high.length() - 3, 4);
                        weather->today.fengli = today_weather.value("fengli").toString();
                        weather->today.fengli.remove(0, 8);
                        weather->today.fengli.remove(weather->today.fengli.length() - 2, 2);
                        weather->today.fengxiang = today_weather.value("fengxiang").toString();
                        /* day1 */
                        today_weather = value.toArray().at(1).toObject();
                        weather->day1.date = today_weather.value("date").toString();
                        weather->day1.type = today_weather.value("type").toString();
                        weather->day1.low = today_weather.value("low").toString();
                        weather->day1.high = today_weather.value("high").toString();
                        weather->day1.low = weather->day1.low.mid(weather->day1.low.length() - 3, 4) ;
                        weather->day1.high = weather->day1.high.mid(weather->day1.high.length() - 3, 4);
                        weather->day1.fengli = today_weather.value("fengli").toString();
                        weather->day1.fengli.remove(0, 8);
                        weather->day1.fengli.remove(weather->day1.fengli.length() - 2, 2);
                        weather->day1.fengxiang = today_weather.value("fengxiang").toString();
                        /* day2 */
                        today_weather = value.toArray().at(2).toObject();
                        weather->day2.date = today_weather.value("date").toString();
                        weather->day2.type = today_weather.value("type").toString();
                        weather->day2.low = today_weather.value("low").toString();
                        weather->day2.high = today_weather.value("high").toString();
                        weather->day2.low = weather->day2.low.mid(weather->day2.low.length() - 3, 4) ;
                        weather->day2.high = weather->day2.high.mid(weather->day2.high.length() - 3, 4);
                        weather->day2.fengli = today_weather.value("fengli").toString();
                        weather->day2.fengli.remove(0, 8);
                        weather->day2.fengli.remove(weather->day2.fengli.length() - 2, 2);
                        weather->day2.fengxiang = today_weather.value("fengxiang").toString();
                        /* day3 */
                        today_weather = value.toArray().at(3).toObject();
                        weather->day3.date = today_weather.value("date").toString();
                        weather->day3.type = today_weather.value("type").toString();
                        weather->day3.low = today_weather.value("low").toString();
                        weather->day3.high = today_weather.value("high").toString();
                        weather->day3.low = weather->day3.low.mid(weather->day3.low.length() - 3, 4) ;
                        weather->day3.high = weather->day3.high.mid(weather->day3.high.length() - 3, 4);
                        weather->day3.fengli = today_weather.value("fengli").toString();
                        weather->day3.fengli.remove(0, 8);
                        weather->day3.fengli.remove(weather->day3.fengli.length() - 2, 2);
                        weather->day3.fengxiang = today_weather.value("fengxiang").toString();
                        /* day4 */
                        today_weather = value.toArray().at(4).toObject();
                        weather->day4.date = today_weather.value("date").toString();
                        weather->day4.type = today_weather.value("type").toString();
                        weather->day4.low = today_weather.value("low").toString();
                        weather->day4.high = today_weather.value("high").toString();
                        weather->day4.low = weather->day4.low.mid(weather->day4.low.length() - 3, 4) ;
                        weather->day4.high = weather->day4.high.mid(weather->day4.high.length() - 3, 4);
                        weather->day4.fengli = today_weather.value("fengli").toString();
                        weather->day4.fengli.remove(0, 8);
                        weather->day4.fengli.remove(weather->day4.fengli.length() - 2, 2);
                        weather->day4.fengxiang = today_weather.value("fengxiang").toString();
                    }
                }
                if(object_data.contains("yesterday")) {
                    QJsonObject value = object_data.value("yesterday").toObject();
                    weather->yesterday.date = value.value("date").toString();
                    weather->yesterday.low = value.value("low").toString();
                    weather->yesterday.high = value.value("high").toString();
                    weather->yesterday.low = weather->yesterday.low.mid(weather->yesterday.low.length() - 3, 4) ;
                    weather->yesterday.high = weather->yesterday.high.mid(weather->yesterday.high.length() - 3, 4);
                    weather->yesterday.type = value.value("type").toString();
                    weather->yesterday.fengli = value.value("fl").toString();
                    weather->yesterday.fengli.remove(0, 8);
                    weather->yesterday.fengli.remove(weather->yesterday.fengli.length() - 2, 2);
                    weather->yesterday.fengxiang = value.value("fx").toString();
                }
                weather->city = object_data.value("city").toString();
                weather->wendu = object_data.value("wendu").toString();
                weather->ganmao = object_data.value("ganmao").toString();
                weather->status = object_data.value("status").toString();
                weather->desc = object_data.value("desc").toString();
            }
        }
    } else {
        qDebug() << "json_recv is NULL or is not a object !!";
    }
}

void xqat_weather::set_Controls(struct weather_t *weather, QTableWidget *tableWidget, QLabel *label)
{
    tableWidget->clearContents();
    /* yesterday */
    tableWidget->setItem(0, 0, new QTableWidgetItem(weather->yesterday.date));
    tableWidget->setItem(1, 0, new QTableWidgetItem(weather->yesterday.type));
    tableWidget->setItem(2, 0, new QTableWidgetItem(weather->yesterday.low + "~" + weather->yesterday.high));
    tableWidget->setItem(3, 0, new QTableWidgetItem(weather->yesterday.fengxiang));
    tableWidget->setItem(4, 0, new QTableWidgetItem(weather->yesterday.fengli));
    /* today */
    tableWidget->setItem(0, 1, new QTableWidgetItem(weather->today.date));
    tableWidget->setItem(1, 1, new QTableWidgetItem(weather->today.type));
    tableWidget->setItem(2, 1, new QTableWidgetItem(weather->today.low + "~" + weather->today.high));
    tableWidget->setItem(3, 1, new QTableWidgetItem(weather->today.fengxiang));
    tableWidget->setItem(4, 1, new QTableWidgetItem(weather->today.fengli));
    /* day1 */
    tableWidget->setItem(0, 2, new QTableWidgetItem(weather->day1.date));
    tableWidget->setItem(1, 2, new QTableWidgetItem(weather->day1.type));
    tableWidget->setItem(2, 2, new QTableWidgetItem(weather->day1.low + "~" + weather->day1.high));
    tableWidget->setItem(3, 2, new QTableWidgetItem(weather->day1.fengxiang));
    tableWidget->setItem(4, 2, new QTableWidgetItem(weather->day1.fengli));
    /* day2 */
    tableWidget->setItem(0, 3, new QTableWidgetItem(weather->day2.date));
    tableWidget->setItem(1, 3, new QTableWidgetItem(weather->day2.type));
    tableWidget->setItem(2, 3, new QTableWidgetItem(weather->day2.low + "~" + weather->day2.high));
    tableWidget->setItem(3, 3, new QTableWidgetItem(weather->day2.fengxiang));
    tableWidget->setItem(4, 3, new QTableWidgetItem(weather->day2.fengli));
    /* day3 */
    tableWidget->setItem(0, 4, new QTableWidgetItem(weather->day3.date));
    tableWidget->setItem(1, 4, new QTableWidgetItem(weather->day3.type));
    tableWidget->setItem(2, 4, new QTableWidgetItem(weather->day3.low + "~" + weather->day3.high));
    tableWidget->setItem(3, 4, new QTableWidgetItem(weather->day3.fengxiang));
    tableWidget->setItem(4, 4, new QTableWidgetItem(weather->day3.fengli));
    /* day4 */
    tableWidget->setItem(0, 5, new QTableWidgetItem(weather->day4.date));
    tableWidget->setItem(1, 5, new QTableWidgetItem(weather->day4.type));
    tableWidget->setItem(2, 5, new QTableWidgetItem(weather->day4.low + "~" + weather->day4.high));
    tableWidget->setItem(3, 5, new QTableWidgetItem(weather->day4.fengxiang));
    tableWidget->setItem(4, 5, new QTableWidgetItem(weather->day4.fengli));
    for(int i = 0; i < 5; i++) {
        for(int j = 0; j < 6; j++) {
            tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        }
    }
    label->setText(weather->today.type + " " + weather->wendu + "℃");
}
